package com.blogspot.sankdadevildaru.filelisthandler;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class ModifiedFilesLister {
	
	int capacity;
	
	public ModifiedFilesLister() {
	}
	
	public ModifiedFilesLister(int cap) {
		 capacity = cap;
	}
	
	public ArrayList getModifiedFilesList() throws IOException {
		File f = new File("C:\\FileList");
		String [] files = f.list();
		
		ArrayList<String> filesList = new ArrayList<String>();
		for(int i=0; i<files.length; i++)
			filesList.add(f.getAbsolutePath() + "\\" + files[i]);
		Collections.sort(filesList);
		
		String oldFileName = filesList.get(filesList.size() - 2);
		String newFileName = filesList.get(filesList.size() - 1);
		
		filesList = null;
		f = null;
		
		String str, oldStr;
		BufferedReader br;
		br = new BufferedReader(new FileReader(oldFileName));
		
		//Use any Collection implementation to compare execution time difference
		//eg. ... new TreeSet<String>(); 
		Collection<String> oldFiles = new LinkedHashSet<String>();
		
		str = br.readLine();
		while(str != null) {
			oldFiles.add(str);
			str = br.readLine();
		}
		br.close();
		
		br = new BufferedReader(new FileReader(newFileName));
		
		ArrayList<String> modifiedFiles = new ArrayList<String>();
		StringTokenizer oldFileNameContents = null, newFileNameContents = null, tmpFileNameContents = null;
		Iterator<String> itr;
		
		str = br.readLine();
		while(str != null) {
			newFileNameContents = new StringTokenizer(str, "|");
			tmpFileNameContents = new StringTokenizer(str, "|");
			itr = oldFiles.iterator();
			
			if(oldFiles.contains(str)){
				while(itr.hasNext()){
					oldStr = (String)itr.next();
					
					if(oldStr != null && oldStr.equals(str)){
						oldFileNameContents = new StringTokenizer(oldStr, "|");
						
						oldFileNameContents.nextToken();
						newFileNameContents.nextToken();
						
						if(!(oldFileNameContents.nextToken()).equals(newFileNameContents.nextToken())) {
							modifiedFiles.add(tmpFileNameContents.nextToken());
							itr.remove();
						}
						break;
					}
				}
			}
			else
				modifiedFiles.add(tmpFileNameContents.nextToken());
			
			str = br.readLine();
		}
		br.close();
		
		return modifiedFiles;
	}
	
	public static void main(String[] args) throws IOException {
		ModifiedFilesLister mfl = new ModifiedFilesLister();
		
		long startTime = System.nanoTime();
		ArrayList al = mfl.getModifiedFilesList();
		long endTime = System.nanoTime();
			
		System.out.println("\n Time Taken: " + (endTime - startTime) + " nano seconds");
				
		PrintWriter pw = new PrintWriter(new FileWriter("C:\\modifiedFilesList.txt"));
		for(int i=0; i<al.size(); i++)
			pw.println(al.get(i));
		pw.close();
	}

}
